
const app = getApp();
const api = app.apiInstance
import moment from '../../public/utils/moment'
import { getUserInfo } from '../../utils/store';
const sapid =getUserInfo().siocSapNo

Page({  
  data: {
    subStatusList: [
      {
        value: 'Complete',
        label: '完成'
      },
      {
        value: 'NotYetStarted',
        label: '未开始'
      },
      {
        value: 'Pass',
        label: '通过'
      },
      {
        value: 'Reject',
        label: '拒绝'
      },
    ],
    searchParams:{
      pageNum:'0',
      pageSize:'99999',
      sapId:sapid
    },

    statusList: [
      {
        value: 'all',
        label: '全部'
      },
      {
        value: 'AcceptanceCompleted',
        label: '完成验收'
      },
      {
        value: 'Cancellation',
        label: '作废'
      },
      {
        value: 'Reject',
        label: '拒绝'
      },
      {
        value: 'ToBeAcceptance',
        label: '待验收'
      },
      {
        value: 'ToBeConfirmed',
        label: '待确认'
      },
      {
        value: 'ToBeReviewed',
        label: '待审核'
      }
    ],
    searchJinDuIndex:0,
    isShowSearch:false,
    dataList:[],
    staffList:[],
    toggle:false,
    swipoutVisible:false
  },
  async loadData(){

    const staffList = await api.get(`/v1/openApi/employee/search/staffList`);
    this.setData({
     staffList:staffList.rows
   })

    
   this.getList();


   
  },

  onShow(){
    this.loadData();
  },

  // onLoad(){
  //   this.loadData();
  // },


  async getList (){
    const dataList = await api.post("/v1/openApi/resource/change/pageList",this.data.searchParams);
    dataList.data.records.forEach(row=>{
        row.dangQianJbr =  row.processStatuses&&row.processStatuses.map(item => this.data.staffList.find(subitem => subitem.sapId ==
        item.auditor)?.staffName).join(',')
        row.dangQianJu =  this.data.statusList.find(item=>item.value == row.status)?.label
          row.checkRights = this.checkRights(row)
        row.isCanEdit = (row.createBy==sapid) && (row.status=='ToBeConfirmed'||row.status=='Reject');
        row.operateWidth = this.operateWidth(row)
      })
      console.log('dataList.data.records',dataList.data.records)
    this.setData({
      resList:dataList.data.records
    })
  },

  setKeyword(e) {
    this.setData({
      'searchParams.name': e.detail.value
    })
  },

  

  operateWidth(row){
    if(row.isCanEdit){
      if(row.checkRights =='shenHe'|| row.checkRights == 'queRen'||row.checkRights == 'yanShou'){
        return '360'
      }
      else{
        return '240'
      }
    }
    else{
      return'120'
    }
  },

  gotoDetail(e){
    wx.navigateTo({
      url: '/subPages/bianGengSqd/bianGengSqd?isEdit=false&id='+e.currentTarget.dataset.item.id,
      
    });
  },

  gotoExtraDetail(e){
    console.log(e.currentTarget.dataset.item)

    wx.navigateTo({
      url: '/subPages/bianGengExtraDetail/bianGengExtraDetail?id='+e.currentTarget.dataset.item.id,
    });
  },

  handleEdit(e){
    console.log(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '/subPages/bianGengSqd/bianGengSqd?isEdit=true&id='+e.currentTarget.dataset.item.id,
      
    });
  },

  handleZuoFei(e){
    const that = this;
    wx.showModal({
      title: '提示',
      content: '是否删除',
      showCancel:true,
      confirmText: '确定',
      success(res) {
       if(res.confirm){
        api.post(`/v1/openApi/resource/change/cancellation/${e.currentTarget.dataset.item.id}/${sapid}`).then(res=>{
          that.setData({
            swipoutVisible:false,
            resList:[]
          })

          setTimeout(()=>{
            that.getList();
          },0)
          
        })
       }
      },
      fail(res) {
        console.log('res2',res)
      }
    })
    

  },

  handleCancel2 () {
    this.setData({
      swipoutVisible: false,
        toggle : this.data.toggle ? false : true
    });
    console.log( this.data.toggle,111111111 )
},

  handleShenHe(e){
    wx.navigateTo({
      url: '/subPages/bianGengShenHe/bianGengShenHe?id='+e.currentTarget.dataset.item.id,
      
    });
  },

  handleQueRen(e){
    console.log('e',e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '/subPages/bianGengQueRen/bianGengQueRen?id='+e.currentTarget.dataset.item.id,
    });
  },

  handleYs(e){
    wx.navigateTo({
      url: '/subPages/bianGengYanShou/bianGengYanShou?id='+e.currentTarget.dataset.item.id,

    });
  },

  

  checkRights(row){
    //确认权限
    if(row.processStatuses&&row.processStatuses.some(item => item.remark && item.remark == '确认')&&row.processStatuses.some(item => item.auditor ==sapid)){
      return 'queRen'
    }
    if(row.processStatuses&&row.processStatuses.some(item => item.remark && item.remark.indexOf('审核') > 0)&&row.processStatuses.some(item => item.auditor ==sapid)){
      return 'shenHe'
    }
    if(row.processStatuses&&row.processStatuses.some(item => item.remark && item.remark.indexOf('验收') > 0)&&row.processStatuses.some(item => item.auditor ==sapid)){
      return 'yanShou'
    }
  },

  showDangQianJd(row){
    return this.data.statusList.find(item=>item.value == row.status)?.label
  },

  showDangQianJbr(row){
    return row.processStatuses&&row.processStatuses.map(item => this.data.staffList.find(subitem => subitem.sapId ==
      item.auditor)?.staffName).join(',')
  },

  searchJinDuChange(e){

    const params = this.data.searchParams;
    params.status = this.data.statusList[e.detail.value].value
    e.detail.value==0&&delete params.status

    this.setData({
      searchJinDuIndex:e.detail.value,
      searchParams:params

    })
    this.getList();
  },
 
  showSearch(){
    this.setData({
      isShowSearch: true
    });
    
  },

  handleOK(){
    this.setData({
      isShowSearch: false
    });
  },

  handleCancel(){
    this.setData({
      isShowSearch: false
    });
  },

  doApplication(){
    wx.navigateTo({
      url: '/subPages/bianGengSqd/bianGengSqd',
      
    });
  }

})
